package wikisearch.similarity;

import java.util.StringTokenizer;

public class CosineScoreFunction extends ScoreFunction {

	public double calculateScore(String [] first, int fbeginIdx, int fendIdx, 
								String [] second, int sbeginIdx, int sendIdx) {
		
		int flen = first.length;
		int slen = second.length;
		int coOccur = 0;
		
		for (int f = fbeginIdx; f < fendIdx; f++) {
			for (int s = sbeginIdx; s < sendIdx; s++) {
				if (first[f].equals(second[s])) {
					StringTokenizer st = new StringTokenizer(first[f]);
					coOccur += st.countTokens();
					coOccur++;
				}
			}
		}
		
		return ( (double)coOccur ) / ( (double)(flen * slen) );
	}

}
